package Util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author letere
 * @create 2020-12-25 11:04
 */
public class Utils {
    /**
     * 读取文件内容，将每一行文本封装为字符串集合
     * @return
     * @throws Exception
     */
    private static List<String> readFile(){
        String filePath = "D:\\Java files\\gitClone\\ex1\\position.txt";
        List<String> texts = null;
        BufferedReader br = null;
        //异常处理
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath))); //创建读取文件流
            texts = new ArrayList<String>(); //创建集合存储字符串

            while(true) {
                String text = br.readLine();
                if(text == null) { //直到下一行没有内容，退出循环
                    break;
                }
                texts.add(text);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return texts;
    }

    /**
     * 创建城市Id集合
     * @return
     */
    public static List<Integer> getCitys() {
        Integer cityQuantity = Integer.valueOf(readFile().get(0));
        List<Integer> citys = new ArrayList<Integer>();
        for(int i=0; i<cityQuantity; i++) {
            citys.add(i+1);
        }
        return citys;
    }

    /**
     * 创建距离矩阵
     * @return
     */
    public static int[][] getDistanceMatrix() {
        List<String> texts = readFile();
        Integer cityQuantity = Integer.valueOf(texts.get(0));
        int[][] distanceMatrix = new int[cityQuantity][cityQuantity];

        for(int i=0; i<cityQuantity; i++) {
            distanceMatrix[i][i] = 0;
            for (int j=i+1; j<cityQuantity; j++) {
                //按空格分割字符串
                String[] city1 = texts.get(i + 1).split(" ");
                String[] city2 = texts.get(j + 1).split(" ");
                //两点距离公式（整数方便查看）
                double distance = Math.pow((Integer.valueOf(city1[1]) - Integer.valueOf(city2[1])), 2) + Math.pow((Integer.valueOf(city1[2]) - Integer.valueOf(city2[2])), 2);
                distanceMatrix[i][j] = (int) Math.sqrt(distance);
                distanceMatrix[j][i] = distanceMatrix[i][j];
            }
        }
        return distanceMatrix;
    }

    /**
     * 创建时间矩阵
     * @return
     */
    public static int[][] getTimeMatrix() {
        List<String> texts = readFile();
        Integer cityQuantity = Integer.valueOf(texts.get(0));
        int[][] timeMatrix = new int[cityQuantity][cityQuantity];

        for(int i=0; i<cityQuantity; i++) {
            timeMatrix[i][i] = 0;
            for (int j=i+1; j<cityQuantity; j++) {
                //按空格分割字符串
                String[] time1 = texts.get(i + 1).split(" ");
                String[] time2 = texts.get(j + 1).split(" ");
                //两点时间公式（整数方便查看）
                double time = Math.pow((Integer.valueOf(time1[3]) - Integer.valueOf(time2[3])), 2);
                timeMatrix[i][j] = (int) Math.sqrt(time);
                timeMatrix[j][i] = timeMatrix[i][j];
            }
        }
        return timeMatrix;
    }


    /**
     * 初始化信息素矩阵
     * @param value 初始值
     * @return
     */
    public static double[][] getPheromoneMatrix(double value) {
        Integer cityQuantity = Integer.valueOf(readFile().get(0));
        double[][] pheromoneMatrix = new double[cityQuantity][cityQuantity];
        for(int i=0; i<cityQuantity; i++) {
            for(int j=0; j<cityQuantity; j++) {
                pheromoneMatrix[i][j] = value; //赋予初始信息素值
            }
        }
        return pheromoneMatrix;
    }
}
